/*
 * Copyright 2010-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.pathlet.core.impl;

import org.pathlet.core.config.def.BeanResourceConfig;
import org.pathlet.core.*;
import org.pathlet.core.exception.ResourceAccessException;

/**
 *
 * @author Charlie Zhang
 *
 */
public class PathBeanResourceFactory implements PathResourceFactory<BeanResource> {

	private PathletContainer container;

	private PathPattern pathPattern = MATCH_PATTERN_ALL;

	private PathPattern typePattern = TYPE_MATCH_PATTERN_ALL;

	private String defaultScope;

	// The base path will append before parse path. For instance: <br/>
	// If this argument is "/service", the all resource path which build from this factory will look like "/service/somepath".
	private Path basePath;

	//The base java package to find bean class. This factory will find the classes under the descendants of this package.
	private String basePackage;

	// A customize class to convert from resourcePath to javaBean Define
	private BeanPathConverter pathConverter;

	public PathletContainer getContainer() {
		return this.container;
	}

	public void setContainer(PathletContainer container) {
		this.container = container;
	}

	public PathPattern getPathPattern() {
		return this.pathPattern;
	}

	public void setPathPattern(PathPattern pathPattern) {
		this.pathPattern = pathPattern;
	}

	public PathPattern getTypePattern() {
		return typePattern;
	}

	public void setTypePattern(PathPattern typePattern) {
		this.typePattern = typePattern;
		this.typePattern.setPathSeparator(".");
	}

	public String getDefaultScope() {
		return defaultScope;
	}

	public void setDefaultScope(String defaultScope) {
		this.defaultScope = defaultScope;
	}

	public Path getBasePath() {
		return basePath;
	}

	public void setBasePath(Path basePath) {
		this.basePath = basePath;
	}

	public String getBasePackage() {
		return basePackage;
	}

	public void setBasePackage(String basePackage) {
		this.basePackage = basePackage;
	}

	public BeanPathConverter getPathConverter() {
		return pathConverter;
	}

	public void setPathConverter(BeanPathConverter pathConverter) {
		this.pathConverter = pathConverter;
	}

	public BeanResource createResource(Path resourcePath) {

		//Created the new BeanResource by resourcePath
		try {
			String className = pathConverter.convertToType(resourcePath, basePath, basePackage);
			BeanResourceConfig config = new BeanResourceConfig();
			config.setPath(resourcePath);
			config.setClassName(className);
			config.setScope(getDefaultScope());


			//BeanResourceConfigHandler configHandler = new BeanResourceConfigHandler(getContainer(), config, this.getDefaultScope());

			return new BeanResource(this, config);
		}
		catch(Exception e) {
			throw new ResourceAccessException("Failed to get resource path=" + resourcePath, e, resourcePath);
		}
	}

	@Override
	public Path getPathByType(String type) {
		return pathConverter.converToPath(type, basePath, basePackage);
	}

}
